package com.bosc.modeling.interceptor;

import com.bosc.modeling.pojo.gen.UserEntity;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 定义拦截器
 */
@Component
//@Log4j
public class ConfigInterceptor implements HandlerInterceptor {
    Logger log = Logger.getLogger(ConfigInterceptor.class);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        // 从session中获取用户信息
        UserEntity user = (UserEntity) session.getAttribute("user");
        // session过期
        if(user == null){
            log.info(">>>session过期, 跳至登录页");
            log.info(request.getServletPath());
            if (request.getServletPath().contains("bg")){
                response.sendRedirect("/html/bg/login.html");
            }else{
                response.sendRedirect("/index.html");
            }
            return false;
        }else{
            session.setAttribute("user", user);
            return true;
        }
    }
}
